{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# transform 常见使用方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "from torchvision import transforms\n",
    "from torch.utils.tensorboard import SummaryWriter\n",
    "from PIL import Image\n",
    "\n",
    "img_path = 'datasets/xiaomao.jpg'\n",
    "img_PIL = Image.open(img_path)\n",
    "print(img_PIL)\n",
    "\n",
    "writer = SummaryWriter('logs')\n",
    "\n",
    "transforms_tool = transforms.ToTensor()\n",
    "\n",
    "img_tensor = transforms_tool(img_PIL)\n",
    "\n",
    "writer.add_image('tensor_cat', img_tensor)\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "# Normalize\n",
    "\n",
    "print(img_tensor[0][0][0])\n",
    "\n",
    "tensor_norm = transforms.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]);\n",
    "img_norm = tensor_norm(img_tensor)\n",
    "print(img_norm[0][0][0])\n",
    "\n",
    "writer.add_image('img_norm_cat', img_norm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "# resize 方法\n",
    "\n",
    "print(img_PIL.size)\n",
    "trans_resize = transforms.Resize((512, 512))\n",
    "img_resize = trans_resize(img_PIL)\n",
    "print(img_resize.size)\n",
    "\n",
    "img_resize_tensor = transforms.ToTensor()(img_resize)\n",
    "writer.add_image('resize_cat', img_resize_tensor)\n",
    "print(img_resize)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "#  Compose 方法\n",
    "\n",
    "trans_resize_2 = transforms.Resize(512)\n",
    "trans_compose = transforms.Compose([trans_resize_2, transforms_tool])\n",
    "img_resize_2 = trans_compose(img_PIL)\n",
    "\n",
    "writer.add_image('resize_2', img_resize_2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "metadata": {}
   },
   "outputs": [],
   "source": [
    "writer.close()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
